using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Colorprj.Account ;
using Colorprj.Common;
using Colorprj.Masters;
namespace Colorprj
{
    public partial class frmPayment : Form
    {
        AccountManager am = new AccountManager();
        BuyerManager bm = new BuyerManager(); 
        public frmPayment()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private Payment getformData() {
           
            Payment p = new Payment();
            p.Amount = Utility.getDecimalVal(txtAmount.Text);
            p.BuyerId = Utility.getIntVal(cboBuyer.SelectedValue);
            p.Remarks = txtRemarks.Text;
            p.PayDate = dtpPayDate.Value;
            if (rdoCash.Checked)
            {
                p.PayMentType = true;
                p.ChequeDate = null;
            }
            else { 
                p.PayMentType = false;
                p.ChequeDate = dtpChequeDt.Value ;
                p.ChequeNo = txtChequeNo.Text;
            
            }
            return p;
        }

        public void ClearForm(Control.ControlCollection coll)
        {
            // Control.ControlCollection coll = this.Controls;
            foreach (Control c in coll)
            {
                if (c.HasChildren) ClearForm(c.Controls);
                if (c is TextBox) c.Text = string.Empty;
                //if (c is DataGridView) { ((DataGridView)c).Rows.Clear(); }
                if (c is ComboBox)
                {
                    ((ComboBox)c).SelectedIndex = -1;
                }
            }
        }
        private void setAddMode()
        {

            btnDelete.Enabled = false;
            btnUpdate.Enabled = false;
            btnSave.Enabled = true;
            dataGridView1.DataSource = am.GetAllPayments().Tables[0];

        }
        private void setEditDelMode()
        {

            btnDelete.Enabled = false;
            btnUpdate.Enabled = false;
            btnSave.Enabled = false;


        }


        private void NumericKeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;


            if (!(char.IsDigit(e.KeyChar) | char.IsControl(e.KeyChar) | (e.KeyChar.Equals('.') & tb.Text.IndexOf(".") < 0)))
            {
                e.Handled = true;

            }


            if ((tb.SelectionStart > tb.Text.Length - 2 & tb.Text.IndexOf(".") >= 0 & tb.Text.IndexOf(".") + 3 == tb.Text.Length))
            {
                e.Handled = true;

            }


            if (e.Handled == false & tb.Text.Length == 0)
            {
                tb.Text = "" + e.KeyChar;

                e.Handled = true;

                tb.SelectionStart = 2;

            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Payment p = getformData();
            if (p.BuyerId == 0) {
                MessageBox.Show("please select Buyer");
                return;
            }
            if (p.Amount <= 0)
            {
                MessageBox.Show("Please  input Amount ");
                return;
            }

            if (!p.PayMentType)
            {
                if (p.ChequeNo.Trim().Equals(""))
                {
                    MessageBox.Show("Please  input Cheque No ");

                    return;
                }
            }

            try
            {

                am.AddPayment(p);
                setAddMode();
                ClearForm(this.Controls);
                MessageBox.Show("Record Saved !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

             

        }

        private void frmPayment_Load(object sender, EventArgs e)
        {
            cboBuyer.DataSource = bm.GetAllBuyers().Tables[0];
            cboBuyer.DisplayMember = "BuyerName";
            cboBuyer.ValueMember = "BuyerId";
            setAddMode();
            dataGridView1.DataSource = am.GetAllPayments().Tables[0] ;  
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            setEditDelMode();

            txtAmount.Text = dataGridView1["amount", e.RowIndex].Value.ToString();
            txtRecNo.Text   = dataGridView1["PayId", e.RowIndex].Value.ToString();
            cboBuyer.SelectedValue = Convert.ToInt16( dataGridView1["Buyerid", e.RowIndex].Value.ToString());
            txtRemarks.Text  = dataGridView1["Remarks", e.RowIndex].Value.ToString();
            dtpPayDate.Value = Convert.ToDateTime(dataGridView1["payDate", e.RowIndex].Value.ToString());
            bool paymentType=  Convert.ToBoolean(dataGridView1["payMentType", e.RowIndex].Value.ToString());
            if (!paymentType)
            {
                rdoCheque.Checked = true;
                dtpChequeDt.Value = Convert.ToDateTime(dataGridView1["ChequeDate", e.RowIndex].Value.ToString());
                txtChequeNo.Text = dataGridView1["ChequeNo", e.RowIndex].Value.ToString();
            }
            else {
                rdoCash.Checked = true;
                dtpChequeDt.Checked  = false;
                txtChequeNo.Text = "";//dataGridView1["ChequeNo", e.RowIndex].Value.ToString();
          
            }
          

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            setAddMode();
            ClearForm(this.Controls);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }

        private void rdoCash_CheckedChanged(object sender, EventArgs e)
        {
            txtChequeNo.Text = "";
            txtChequeNo.Enabled = false;
            dtpChequeDt.Checked = false; 
        }

        private void rdoCheque_CheckedChanged(object sender, EventArgs e)
        {
            txtChequeNo.Enabled = true ;
            dtpChequeDt.Checked = true; 
        }
    }
}